package my.algorithms.mcorrea.topcoder;

/**
 *
 * @author mcorrea
	* Magical Girl Lein is observing ants. 
	* There are N ants. At first, they have integer coordinates in the Cartesian plane. 
	* More precisely, ant i starts at the point (x[i], y[i]). All ants move at the same speed. 
	* Each ant moves in one of the four basic directions. (I.e., either parallel to the x axis or parallel 
	* to the y axis.) When 2 or more ants meet at the same time, these ants disappear. 

	You are given two int[]s x and y, containing N elements each, and a String direction, containing N 
	* characters. Character i of direction encodes the direction in which ant i is going: 
	* 
	* 'N' means north (y coordinate increases),
	* 'E' means east (x coordinate increases),
	* 'S' means south (y coordinate decreases),
	* and 'W' means west (x coordinate decreases).
	* Return the number of ants that still exist after the last meeting occurs.
 */
public class AntsMeet {

    public static void main(String[] args) {
		System.out.println( countAnts(new int[]{0,10,20,30}, new int[]{0,10,20,30}, "NWNE")  );
		System.out.println( countAnts(new int[]{-10,0,0,10}, new int[]{0,-10,10,0}, "NEWS")  );
		System.out.println( countAnts(new int[]{-1,-1,-1,0,0,0,1,1,1}, new int[]{-1,0,1,-1,0,1,-1,0,1},"ESEWNNEWW")  );
		
		System.out.println( countAnts(new int[]{4,7,6,2,6,5,7,7,8,4,7,8,8,8,5,4,8,9,1,5,9,3,4,0,0,1,0,7,2,6,9,6,3,0,5,5,1,2,0,4,9,7,7,1,8,1,9,2,7,3},
					new int[]{2,3,0,6,8,4,9,0,5,0,2,4,3,8,1,5,0,7,3,7,0,9,8,1,9,4,7,8,1,1,6,6,6,2,8,5,1,9,0,1,1,1,7,0,2,5,4,7,5,3}, 
					"SSNWSWSENSWSESWEWSWSENWNNNESWSWSWWSSWEEWWNWWWNWENN")  );
    }
	
	public static int countAnts(int[] x, int[] y, String direction){
		int ants = x.length;//number of ants at the beggining
		boolean[] die = new boolean[ants];
		
		//current starting point
		for(int i=0; i<ants; i++){
			//search for other that croses
			if(!die[i]){
				for(int j = i+1; j<ants; j++){
					if(!die[j]){
						char diri = direction.charAt(i);
						char dirj = direction.charAt(j);
						//option to for ants to disappear
						if( diri == 'N' && dirj == 'W' && x[j] > x[i] && y[j] > y[i] ){
							die[i] = true;
							die[j] = true;
						}else if( diri == 'W' && dirj == 'N' && x[i] > x[j] && y[i] > y[j] ){
							die[i] = true;
							die[j] = true;
						}else if( diri == 'N' && dirj=='E' && y[i] < y[j] && x[i] > x[j] ){
							die[i] = true;
							die[j] = true;
						}else if( diri == 'E' && dirj=='N' && y[i] > y[j] && x[i] < x[j]  ){
							die[i] = true;
							die[j] = true;	
						}else if( diri == 'S' && dirj=='W' && x[i] < x[j] && y[i] > y[j] ){
							die[i] = true;
							die[j] = true;
						}else if( diri == 'W' && dirj== 'S' && x[i] > x[j] && y[i] < y[j] ){
							die[i] = true;
							die[j] = true;
						}else if( diri == 'S' && dirj == 'E'  && y[i] > y[j] && x[i] > x[j]  ){
							die[i] = true;
							die[j] = true;
						}else if( diri == 'E' && dirj == 'S' && y[i] < y[j] && x[i] < x[j] ){
							die[i] = true;
							die[j] = true;
						}
						if(die[i] && die[j])System.out.println("die: ("+x[i]+","+y[i]+") and ("+x[j]+","+y[j]+") ");
					}
				}
			}
		}
		
		int nalive = 0;
		for(boolean isdeath: die){
			if(!isdeath){
				nalive++;
			}
		}
		return nalive;
	}

}
